<?php
ini_set("memory_limit","1024M");
set_time_limit(0);
class Test_con extends Lxbase {
    public  $dbconn_old = array( "host"=>'103.7.222.62', "user"=>'root', "pass"=>'zhs20151117', "name"=>'zqtest');
    public  $dbconn_new = array( "host"=>'103.7.222.31', "user"=>'qyer', "pass"=>'qYf2^8L!ne', "name"=>'qiyueII');

    public function  index($tab=''){
        $db = new Lxdb($this->dbconn31);
        $res = $db->arrdata('*',$tab);
        return $res;
    }

    public function hebing()
    {
        $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'qiyuedb'";
        $db = new Lxdb($this->dbconn41);
        $res = $db->arrdata('TABLE_NAME','INFORMATION_SCHEMA.TABLES',"WHERE TABLE_SCHEMA = 'qiyuedb'");
        array_shift($res);
        array_shift($res);
        array_shift($res);
        array_shift($res);
        foreach($res as $k=>&$v){
            $data = $this->index($v['TABLE_NAME']);
            foreach($data as $a=>&$b){
                foreach($b as $o=>&$p){
                    if(is_integer($o) || $o=='id'){
                       // unset($res[$k][$a][$o]);
                        unset($b[$o]);
                    }
                }
            }
            foreach($data as $aa=>$bb){
                @$db->setdata($v['TABLE_NAME'],$bb);
                echo "2222";
            }

        }
    }

    public function migrate(){
        $o = new Lxdb($this->dbconn_old);
        $n = new Lxdb($this->dbconn_new);
        $res1 = $o->arrdata('*','qy_users');
        foreach($res1 as $k=>$v){
            $username = $v['username'];
               $nameres = $n->viewdata('*','qy_users','where username = "' . $username . '"');
               if(!empty($nameres)){
                    continue;
                }
             $password = '123456';
             $url = 'http://user.zhongsou.com/usercenter/normal/user/2b/signup?username=' . $username . '&password=' . $password;
                // 注册接口
                $rres = file_get_contents($url);
                $rres = json_decode($rres,true);
                if(isset($rres['code']) && $rres['code']==200){
                    //插入企悦用户表
                    $qydata = array();
                    $qydata['username'] = $username;
                    $qydata['uid'] = $rres['uid'];
                    $qydata['status'] = 1;
//                    if($v['token']){
//                        //有token
                    $qydata['token'] = $v['token'];
//                    }else{
//                        //无token
//                        //生成token
//                        $randLength=6;
//                        $chars='abcdefghijklmnopqrstuvwxyz';
//                        $len=strlen($chars);
//                        $randStr='';
//                        for ($i=0;$i<$randLength;$i++){
//                            $randStr.=$chars[rand(0,$len-1)];
//                        }
//                        $tokenvalue=$randStr.time().rand(1000,9999);
//                        $qydata['token'] = $tokenvalue;
//                    }
                    $qydata['hytype'] = $v['hytype'];
                    $qydata['plas'] = $v['plas'];
                    $qydata['start_time'] = $v['start_time'];
                    $qydata['end_time'] = $v['end_time'];
                    $qydata['three_domain'] = $v['three_domain'];
                    $qydata['clientname'] = $v['clientname'];
                    $qydata['appname'] = $v['appname'];
                    $n->setdata('qy_users',$qydata);  //插入新企悦用户表
                    //设置配置信息
                    $ext = array(
                        //版本id
                        'gid' => $v['hytype'],
                        'token' => $v['token'],
                        'plas' => $v['plas']
                    );
                    $data = array(
                        'uid' => $rres['uid'],
                        'projectId' => '1481169291265',
                        'ext' => json_encode($ext)
                    );
                    $this->call_post('http://super.zhongsou.com/super/user/config/saveOrUpdate', $data);
                }else{
                    //注册失败
                    echo $username . '<br/>';
                }

        }
    }

    public function updateuid(){
        $n = new Lxdb($this->dbconn_new);
        $res1 = $n->arrdata('*','qy_users');
        foreach($res1 as $k=>$v){
            $username = $v['username'];
            $password = '123456';
            $url = 'http://user.zhongsou.com/usercenter/normal/user/2b/signup?username=' . $username . '&password=' . $password;
            $rres = file_get_contents($url);
            $rres = json_decode($rres,true);
            if(isset($rres['code']) && $rres['code']==200){
                $uid = $rres['uid'];
                $n->updata('qy_users','uid = "' . $uid . '"','where id = ' . $v['id']);
            }else{
                echo $v['username'].'<br/>';
            }
        }
    }

    public function getuid(){
        $username = 'qy13685767770';
        $password = '123456';
        $url = 'http://user.zhongsou.com/usercenter/normal/user/2b/signup?username=' . $username . '&password=' . $password;
        $rres = file_get_contents($url);
        $rres = json_decode($rres,true);
        var_dump($rres);exit;
    }

    public function updatepuid(){
        $n = new Lxdb($this->dbconn_new);
        $username = 'wangtiancheng';
        $n->updata('qy_users','p_uid="c503aa90eaa67d6b6260734fef5909f6944e94d3"','where id = 650');
    }

    public function deldate(){
        $n = new Lxdb($this->dbconn_new);
        $res = $n->arrdata('*','qy_users');
        foreach($res as $k=>$v){
            $n->deldata('qy_users','where id = ' . $v['id']);
        }
    }

}